package com.sun.dao;

import com.sun.pojo.Comment;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/10
 */
@Mapper
@Repository
public interface CommentDao {

    //根据id查询评论
    @Select("select * from blog.t_comment where id=#{id}")
    List<Comment> selectCommentById(Long id);

    //查询一级评论
    @Select("select * from blog.t_comment where blog_id=#{blogId} and parent_comment_id=-1 order by create_time desc")
    List<Comment> selectCommentByBlogIdTop(Long blogId);

    //查询二级评论
    @Select("select * from blog.t_comment where blog_id=#{blogId} and parent_comment_id=#{parentCommentId} order by create_time desc")
    @Results(id = "CommentMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "blog_id",property = "blogId"),
            @Result(column = "parent_comment_id",property = "parentCommentId"),
            @Result(property = "parentComment",column = "parent_comment_id",
                    one = @One(select = "com.sun.dao.CommentDao.selectCommentById",fetchType = FetchType.LAZY)),
    })
    List<Comment> selectCommentByBlogIdSecond(@Param("blogId") Long blogId,@Param("parentCommentId")Long parentCommentId);

    //新增评论
    @Insert("insert into blog.t_comment (nickname,email,content,avatar,create_time,blog_id,parent_comment_id)\n" +
            "        values (#{nickname},#{email},#{content},#{avatar},#{createTime},#{blogId},#{parentCommentId})")
    int insertComment(Comment comment);

}
